package com.flyco.modules.pms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.pms.entity.PmsProductTag;
import com.flyco.modules.pms.service.IPmsProductTagService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.modules.system.entity.SysPartner;
import com.flyco.modules.system.service.ISysPartnerService;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 商品标签表
 * @Author: flyco
 * @Date: 2022-02-16
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "商品标签表")
@RestController
@RequestMapping("/f/pms/pmsProductTag")
public class PmsProductTagController extends BaseController<PmsProductTag, IPmsProductTagService> {
    @Autowired
    private IPmsProductTagService pmsProductTagService;
    @Autowired
    private ISysPartnerService sysPartnerService;

    /**
     * 分页列表查询
     *
     * @param pmsProductTag
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "商品标签表-分页列表查询")
    @ApiOperation(value = "商品标签表-分页列表查询", notes = "商品标签表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(PmsProductTag pmsProductTag,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<PmsProductTag> queryWrapper = QueryGenerator.initQueryWrapper(pmsProductTag, req.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysPartner partner = sysPartnerService.getById(sysUser.getPartnerId());
        queryWrapper.eq("brand_id", partner.getBrandId());
        Page<PmsProductTag> page = new Page<PmsProductTag>(pageNo, pageSize);
        IPage<PmsProductTag> pageList = pmsProductTagService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param pmsProductTag
     * @return
     */
    @AutoLog(value = "商品标签表-添加")
    @ApiOperation(value = "商品标签表-添加", notes = "商品标签表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PmsProductTag pmsProductTag) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysPartner partner = sysPartnerService.getById(sysUser.getPartnerId());
        pmsProductTag.setBrandId(partner.getBrandId());
        pmsProductTagService.save(pmsProductTag);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param pmsProductTag
     * @return
     */
    @AutoLog(value = "商品标签表-编辑")
    @ApiOperation(value = "商品标签表-编辑", notes = "商品标签表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody PmsProductTag pmsProductTag) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysPartner partner = sysPartnerService.getById(sysUser.getPartnerId());
        pmsProductTag.setBrandId(partner.getBrandId());
        pmsProductTagService.updateById(pmsProductTag);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商品标签表-通过id删除")
    @ApiOperation(value = "商品标签表-通过id删除", notes = "商品标签表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        pmsProductTagService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "商品标签表-批量删除")
    @ApiOperation(value = "商品标签表-批量删除", notes = "商品标签表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.pmsProductTagService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商品标签表-通过id查询")
    @ApiOperation(value = "商品标签表-通过id查询", notes = "商品标签表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        PmsProductTag pmsProductTag = pmsProductTagService.getById(id);
        return Result.ok(pmsProductTag);
    }


    /**
     * 通过productCategoryId查询
     *
     * @param productCategoryId
     * @return
     */
    @AutoLog(value = "商品标签表-通过productCategoryId查询")
    @ApiOperation(value = "商品标签表-通过productCategoryId查询", notes = "商品标签表-通过productCategoryId查询")
    @GetMapping(value = "/queryByProductCategoryId")
    public Result<?> queryByProductCategoryId(@RequestParam(name = "productCategoryId", required = true) String productCategoryId) {
		LambdaQueryWrapper<PmsProductTag> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PmsProductTag::getProductCategoryId, productCategoryId);
		List<PmsProductTag> productTags = pmsProductTagService.list(queryWrapper);
        return Result.ok(productTags);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param pmsProductTag
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PmsProductTag pmsProductTag) {
        return super.exportXls(request, pmsProductTag, PmsProductTag.class, "商品标签表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PmsProductTag.class);
    }

}
